<template>
  <div class="addressList">
    <Header left="arrow" title="收货地址" :from="from" right="ok"></Header>
    <van-address-list
      v-model="chosenAddressId"
      :list="list"
      :disabled-list="disabledList"
      disabled-text="以下地址超出配送范围"
      default-tag-text="默认"
      @add="onAdd"
      @edit="onEdit"
      @select="changeSelect"
    />
  </div>
</template>
<script>
import addressApi from "@/api/address";
import Vue from "vue";
import { AddressList, Toast } from "vant";

Vue.use(AddressList).use(Toast);
import Header from "@/components/Header";
export default {
  name: "addressList",
  components: {
    Header,
  },
  data() {
    return {
      chosenAddressId: "1",
      from: "",
      list: [
        // {
        //   id: "1",
        //   name: "张三",
        //   tel: "13000000000",
        //   address: "浙江省杭州市西湖区文三路 138 号东方通信大厦 7 楼 501 室",
        //   isDefault: true,
        // },
        // {
        //   id: "2",
        //   name: "李四",
        //   tel: "1310000000",
        //   address: "浙江省杭州市拱墅区莫干山路 50 号",
        //   isDefault: false,
        // },
      ],
      disabledList: [
        {
          id: "3",
          name: "test",
          tel: "1320000000",
          address: "浙江省杭州市滨江区江南大道 15 号",
        },
      ],
    };
  },
  computed: {
    storeInfo() {
      return this.$store.getters.storeInfo;
    },
    userInfo() {
      return this.$store.getters.userInfo;
    },
  },
  mounted() {
    this.getAddressList();
  },
  beforeRouteEnter(to, from, next) {
    console.log("beforeRouteEnter", from.fullPath);
    var path = from.fullPath;
    next((vm) => {
      vm.from = path;
    });
  },
  methods: {
    onAdd() {
      //   Toast("新增地址");
      this.$router.push({ path: "addressEdit", params: {} });
    },
    onEdit(item, index) {
      Toast("编辑地址:" + index);
    },
    changeSelect(item, index) {
      console.log("item", item);
      this.$store.dispatch("setCurrentAddress", item);
    },
    //获取地址列表
    getAddressList() {
      var formData = new FormData();
      formData.append("userId", this.userInfo.userId);
      addressApi.getAddressList(formData).then((response) => {
        var date = response.data.data;
        for (let index = 0; index < date.length; index++) {
          const element = date[index];
          element.id = element.addressId;
          element.address = element.addressDetail;
          if (element.isDefault == 1) {
            element.isDefault = true;
          } else {
            element.isDefault = false;
          }
        }
        console.log("addressList", date);
        this.list = date;
      });
    },
  },
};
</script>
<style scoped>
.addressList {
  padding-top: 2.13rem;
  height: 100%;
  display: flex;
  flex-direction: column;
  background-color: #f6f6f6;
}
::v-deep .van-radio__icon {
  font-size: 24px !important;
}
::v-deep .van-address-item .van-radio__icon--checked .van-icon {
  background-color: #dba871;
  border-color: #dba871;
}
::v-deep .van-tag--danger {
  background-color: #dba871;
}
::v-deep .van-button--danger {
  color: #fff;
  background-color: #dba871;
  border: 0.02667rem solid #dba871;
}
</style>